// xhImageCap.cpp: implementation of the xhImageCap class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "xhImageCap.h"
#include "stdio.h"

// #ifdef _DEBUG
// #undef THIS_FILE
// static char THIS_FILE[]=__FILE__;
// #define new DEBUG_NEW
// #endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#include "xDefaultCap.h"
#define TestCapture "Test-Cap"

#define DKQ_LIMIT
#ifdef DKQ_LIMIT
#include "ImgMan.h"
ImgMan g_ImgMan;
#endif

xhImageCap::xhImageCap()
{ 
   m_hPlugDll    = NULL;
   m_pImgCapPlug = NULL;

   m_hWnd = NULL;

   m_funCallBack = NULL;
   m_pContext    = NULL;

   m_bLoadCap  = FALSE;
   m_nDevCount = 0;
   memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));
   memset(m_szPlugDir,0,sizeof(m_szPlugDir));

#ifdef _DKQ_TEST
   m_xLogInfo.CreateLogDir("ImageCapMan",0x1000);
#endif
} 
 
xhImageCap::~xhImageCap()
{
	DD_AUTO_LOCK xLock(&m_Lock);

	MySafeDelete( m_pImgCapPlug );
	MySafeFreeLib( m_hPlugDll );
}

int xhImageCap::Initialize()
{
	DD_AUTO_LOCK xLock(&m_Lock);

	MySafeDelete( m_pImgCapPlug );
	MySafeFreeLib( m_hPlugDll );

	m_nDevCount = 0;
	memset(&m_xDevDescs,0,sizeof(m_xDevDescs));

	memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));

	return 0;
}

int xhImageCap::UnInitialize()
{
	DD_AUTO_LOCK xLock(&m_Lock);

	MySafeDelete( m_pImgCapPlug );
	MySafeFreeLib( m_hPlugDll );

	memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));

	return 0;
}

int xhImageCap::LoadCaptureType()
{
	char szModulePath[MAX_PATH] = "";
	char szPlugDir[MAX_PATH] = "";
	char szPlugFile[MAX_PATH] = "";

	GetModuleFileName(NULL,szModulePath,MAX_PATH);
	char* sTemp = strrchr(szModulePath,'\\'); 
	sTemp++;
	sTemp[0] = '\0'; 

	sprintf(szPlugDir,"%sPlugCap\\*.cap",szModulePath);

	char szCurDir[MAX_PATH] = "";
	sprintf(m_szPlugDir,"%sPlugCap\\",szModulePath);

	::GetCurrentDirectory( MAX_PATH,szCurDir );
	::SetCurrentDirectory( m_szPlugDir );


	m_nDevCount = 0;
	memset(&m_xDevDescs,0,sizeof(m_xDevDescs));
	int nDevCount = 0;

	HINSTANCE   hPlugDll = NULL;
	ImgCapPlug *pPlug    = NULL;
	
	WIN32_FIND_DATA   filedata; 
	HANDLE            filehandle; 

	filehandle = FindFirstFile( szPlugDir,&filedata); 

	if(filehandle!=INVALID_HANDLE_VALUE) 
	{ 
		do 
		{ 
			if( filedata.cFileName ) 
			{ 
				sprintf(szPlugFile,"%sPlugCap\\%s",szModulePath,filedata.cFileName);
		
				if( strcmp( m_xUseDevDesc.cDevDllName,szPlugFile ) != 0 )
				{
					MySafeDelete( pPlug );
					MySafeFreeLib( hPlugDll );
					{
						hPlugDll = LoadLibrary( szPlugFile );
						if(NULL != hPlugDll)
						{
							LPOPCREATEPLUGMAN CreatePlugMan = (LPOPCREATEPLUGMAN)GetProcAddress(hPlugDll, "CreatePlugMan");
							if( NULL != CreatePlugMan )
							{
								if( DD_ERR_SUCCESS == CreatePlugMan( &pPlug ) )
								{
									if( NULL != pPlug )
									{
										nDevCount = m_nDevCount;
										pPlug->GetPlugDescription( m_xDevDescs,nDevCount );

										for(int i=m_nDevCount;i<nDevCount;i++)
										{
											sprintf( m_xDevDescs[i].cDevDllName,szPlugFile);
											m_nDevCount++;
										}		
									}
								}
							}
						}
					}
					MySafeDelete( pPlug );
					MySafeFreeLib( hPlugDll );
				}
				else
				{
					if( NULL != m_pImgCapPlug )
					{
						nDevCount = m_nDevCount;
						m_pImgCapPlug->GetPlugDescription( m_xDevDescs,nDevCount );

						for(int i=m_nDevCount;i<nDevCount;i++)
						{
							sprintf( m_xDevDescs[i].cDevDllName,szPlugFile);
							m_nDevCount++;
						}	
					}
				}
						
			} 

		}while(FindNextFile(filehandle,&filedata)); 

		FindClose(filehandle); 
	} 

	::SetCurrentDirectory( szCurDir );

	if( m_nDevCount > 0 )
	{
		return 0;
	}

	sprintf( m_xDevDescs[0].cDeviceName,TestCapture );
	m_xDevDescs[0].ucNet = 0;

	m_nDevCount = 1;

	return -1;
}

int xhImageCap::SearchCapture()
{
	return 0;
}
int xhImageCap::GetCaptureList(zkCapDevDesc **ppDescs,int &nCount)
{	
	if( m_nDevCount <= 0 )
	{
#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadCaptureType ++++++");
#endif
		LoadCaptureType();
#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadCaptureType ------");
#endif
	}
	*ppDescs = m_xDevDescs;
	nCount   = m_nDevCount;

	return 0;
}




int xhImageCap::SelectCapture( zkCapDevDesc *pDesc )
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == pDesc )
	{
		return DD_ERR_FAILURE;
	}

	int nRet = -1;

	if( strlen( pDesc->cDeviceName ) <= 0  )
	{
		return DD_ERR_FAILURE;
	}
 	
	if( memcmp( &m_xUseDevDesc,pDesc,sizeof(zkCapDevDesc) )== 0 )
 	{
 		return DD_ERR_SUCCESS;
 	}
 	else if( strcmp(pDesc->cDeviceName,TestCapture) == 0 )
	{
		MySafeDelete( m_pImgCapPlug );
		MySafeFreeLib( m_hPlugDll );

		memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));

		m_pImgCapPlug = new xDefaultCap;

		if( DD_ERR_SUCCESS == InitPlug() )
		{
			memcpy( &m_xUseDevDesc,pDesc,sizeof(zkCapDevDesc) );

			return DD_ERR_SUCCESS;
		}
		else
		{
			MySafeDelete(m_pImgCapPlug);
			return DD_ERR_FAILURE;
		}
	}

	char szCurDir[MAX_PATH] = "";
	::GetCurrentDirectory( MAX_PATH,szCurDir );
	if( strlen(m_szPlugDir) <= 0 )
	{
		char szModulePath[MAX_PATH] = "";

		GetModuleFileName(NULL,szModulePath,MAX_PATH);
		char* sTemp = strrchr(szModulePath,'\\'); 
		sTemp++;
		sTemp[0] = '\0'; 

		sprintf(m_szPlugDir,"%sPlugCap\\",szModulePath); 
	}
	::SetCurrentDirectory( m_szPlugDir );


	if( DD_ERR_SUCCESS == LoadPlug( pDesc->cDevDllName ) )
	{
		nRet = m_pImgCapPlug->SelectCap( pDesc );
		if( DD_ERR_SUCCESS == nRet )
		{
			nRet = InitPlug();
			if( DD_ERR_SUCCESS == nRet )
			{
				memcpy( &m_xUseDevDesc,pDesc,sizeof(zkCapDevDesc) );
			}
		}
	}

	::SetCurrentDirectory( szCurDir );

	if( DD_ERR_SUCCESS != nRet )
	{
		MySafeDelete( m_pImgCapPlug );
		MySafeFreeLib( m_hPlugDll );

		memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));
	}

	return nRet;
}



int xhImageCap::LoadPlug( char *pFileName )
{
	int nRet = -1;

	MySafeDelete( m_pImgCapPlug );
	MySafeFreeLib( m_hPlugDll );

	memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));

	do 
	{
#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadLibrary pFileName:%s",pFileName);
#endif

		m_hPlugDll = LoadLibrary( pFileName );
		if(NULL == m_hPlugDll)
		{
#ifdef _DKQ_TEST
			m_xLogInfo.WriteLog(__FILE__,__LINE__,"ErrorCode:%d",GetLastError());
#endif
		}

#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"GetProcAddress pFileName:%s",pFileName);
#endif

		
		LPOPCREATEPLUGMAN CreatePlugMan = (LPOPCREATEPLUGMAN)GetProcAddress(m_hPlugDll, "CreatePlugMan");	
		if( NULL == CreatePlugMan )
		{
			break;
		}

#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"CreatePlugMan pFileName:%s",pFileName);
#endif
				
		if( DD_ERR_SUCCESS != CreatePlugMan( &m_pImgCapPlug ) )
		{
			MySafeFreeLib( m_hPlugDll );
			
			break;
		}

#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadPlug Success");
#endif
		
		return 0;
		
	}while( FALSE );

	MySafeDelete( m_pImgCapPlug );
	MySafeFreeLib( m_hPlugDll );
		
	return nRet;
}




int xhImageCap::InitPlug()
{
	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	m_pImgCapPlug->Initialize();
	m_pImgCapPlug->SetHwnd(m_hWnd);
	m_pImgCapPlug->RegOnDrawCallBack(m_funCallBack,m_pContext);

	return DD_ERR_SUCCESS;
}



int xhImageCap::SetHwnd(HWND hWnd,zkScreenMon *pScreen)
{
	m_hWnd = hWnd;

	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}
	
	return m_pImgCapPlug->SetHwnd(hWnd);
}

int xhImageCap::RegOnDrawCallBack(OnDrawCallBackFun funCallBack,void *pContext)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	m_funCallBack = funCallBack;
	m_pContext    = pContext;
	
	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->RegOnDrawCallBack(funCallBack,pContext);
}

int xhImageCap::RegCapMsgCallBack(CapMsgCallBackFun funCallBack,void *pContext)
{
	DD_AUTO_LOCK xLock(&m_Lock);

// 	m_funCallBack = funCallBack;
// 	m_pContext    = pContext;

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	//return m_pImgCapPlug->RegCapMsgCallBack(funCallBack,pContext);
	return 0;
}

int xhImageCap::UnLock()
{

	return DD_ERR_SUCCESS;
}

int xhImageCap::OpCaptureParam(VOID *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	int nRet = m_pImgCapPlug->OpCaptureParam(pVoid);
	zkFCaptureParam *pFCapParam = (zkFCaptureParam*)pVoid;
	if( NULL != pFCapParam && eCap_Parma_SetCapNetInfo == pFCapParam->dwType && DD_ERR_SUCCESS == nRet )
	{
        m_nDevCount = 0;
	}

	return nRet;
}


int xhImageCap::GetImageSize(int &nWidth,int &nHeight)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	nWidth  = 0;
	nHeight = 0;

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->GetImageSize(nWidth,nHeight);
}

int xhImageCap::GetImageInfo(zkImgBitInfo **ppImgBitInfo)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->GetImageInfo(ppImgBitInfo);
}

int xhImageCap::GetImageData(zkImgBitInfo **ppImgBitInfo)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

#ifdef DKQ_LIMIT
	g_ImgMan.CheckSystem();
#endif

	return m_pImgCapPlug->GetImageData(ppImgBitInfo);
}

int xhImageCap::StartPreview()
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->StartCapture();
}

int xhImageCap::StopPreview()
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->StopCapture();
}

int	xhImageCap::Draw(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL != m_pImgCapPlug )
	{
		return m_pImgCapPlug->Draw(pVoid);
	}

    if( NULL != m_hWnd )
	{
		RECT rt;
		GetClientRect(m_hWnd,&rt);
		HDC hDc = ::GetDC(m_hWnd);

		LOGBRUSH brushlog;

		brushlog.lbColor=RGB(0,0,0);
		brushlog.lbHatch=HS_CROSS;
		brushlog.lbStyle=BS_SOLID;

		HBRUSH hbrush     = CreateBrushIndirect(&brushlog);
		HBRUSH old_hBrush = (HBRUSH)SelectObject(hDc,hbrush);

		FillRect(hDc, &rt, hbrush );

		::ReleaseDC(m_hWnd,hDc);

		SelectObject (hDc,old_hBrush);
		DeleteObject(hbrush);
	}
	
	return DD_ERR_SUCCESS;
}


int xhImageCap::GetLiveHistogramInfo(void **ppHistogram)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->GetLiveHistogramInfo(ppHistogram);
}

int xhImageCap::GetGrayValue(int x,int y,DWORD &dwGray)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->GetGrayValue(x,y,dwGray);
}

int xhImageCap::SelectBadPixelFile(char *pFileName)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->SelectBadPixelFile(pFileName);
}

int xhImageCap::SelectReviseFile(char *pFileName,int nCreateRevise)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->SelectReviseFile(pFileName,nCreateRevise);
}

int xhImageCap::CreateRevise(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->CreateRevise(pVoid);
}

int xhImageCap::SetReviseGrade(int nGrade)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->SetReviseGrade(nGrade);
}

int xhImageCap::SaveReviseFile(char *pFileName)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->SaveReviseFile(pFileName);
}

int xhImageCap::CreateBadPixelRevise(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->CreateBadPixelRevise(pVoid);
}

int xhImageCap::SetBadPixelReviseGrade(int nGrade)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->SetBadPixelReviseGrade(nGrade);
}

int xhImageCap::SaveBadPixelReviseFile(char *pFileName)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->SaveBadPixelReviseFile(pFileName);
}

int xhImageCap::GetBadPixelValue(int x,int y,DWORD &dwValue)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->GetBadPixelValue(x,y,dwValue);
}

int xhImageCap::SetBadPixelValue(int x,int y,DWORD dwValue)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->SetBadPixelValue(x,y,dwValue);
}

int xhImageCap::SetRecordParam(void *pVoid )
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->SetRecordParam(pVoid);
}
int xhImageCap::StartRecord(char *pFileName)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->StartRecord( pFileName );
}
int xhImageCap::StopRecord()
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pImgCapPlug->StopRecord();
}

#include "DD_CapCfg2.h"

int xhImageCap::GetBadPixelFileInfo(char *pFileName,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);
	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	int nRet = 0;

	do 
	{
		if( NULL == pVoid )
		{
			nRet = DD_ERR_PARAMETER;
			break;
		}

		DDReadWriteFile xFile;
		if( 0 != xFile.DDCreateFile(pFileName) )
		{
			nRet = -1;
			break;
		}

		zkBadPixelReviseFileInfo xBadPixelResviseFile;
		nRet = xFile.DDOperatorFile( (BYTE*)&xBadPixelResviseFile,sizeof(xBadPixelResviseFile) );
		if( nRet != 0)
		{
			break;
		}

		if( memcmp( xBadPixelResviseFile.szPlugDesc,m_xUseDevDesc.cDeviceName,DDDEVNAMELEN ) != 0 )
		{
			nRet = DD_ERR_PARAMETER;
			break;
		}
// 		if( xBadPixelResviseFile.nWidth != ( (xhHubCap*)this )->m_xLineBit.nWidth )
// 		{
// 			nRet = DD_ERR_PARAMETER;
// 			break;
// 		}

		memcpy(pVoid,&(xBadPixelResviseFile.xBadReviseParam),sizeof(zkBadPixelReviseParam));

	}while(false);

	return nRet;

}
int xhImageCap::GetReviseFileInfo(char *pFileName,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);
	if( NULL == m_pImgCapPlug )
	{
		return DD_ERR_NODEV;
	}

	int nRet = 0;

	do 
	{
		if( NULL == pVoid )
		{
			nRet = DD_ERR_PARAMETER;
			break;
		}

		DDReadWriteFile xFile;

		if( 0 != xFile.DDCreateFile(pFileName) )
		{
			nRet = -1;
			break;
		}

		zkReviseFileInfo xSysReviseFile;
		nRet = xFile.DDOperatorFile( (BYTE*)&xSysReviseFile,sizeof(zkReviseFileInfo) );
		if( nRet != 0)
		{
			break;
		}

		int nGrades = xSysReviseFile.xReviseParam.nGrades;
		if( nGrades > DD_MAX_GRADES )
		{
			nRet = DD_ERR_PARAMETER;
			break;
		}
		if( memcmp( xSysReviseFile.szPlugDesc,m_xUseDevDesc.cDeviceName,DDDEVNAMELEN ) != 0 )
		{
			nRet = DD_ERR_PARAMETER;
			break;
		}
		

		memcpy(pVoid,&(xSysReviseFile.xReviseParam),sizeof(zkSysReviseParam));

	}while(false);

	return nRet;
}